<?php

ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\GoodsBasicInfo as mGoodsBasicInfo;
use Biz\model\trade\Cart;
use Biz\cache\GoodsSkuId;


$filepath = "/tmp/goodsListInfos.csv";

$fp = fopen($filepath, 'w');

$title = [
	'商家UID',
	'商家名称',
	'商品SPUID',
	'商品名',
	'品牌名',
	'所属类目',
	'售价区间',
	'晒图数',
	'加购数',
	'状态',
];

fputcsv($fp, $title);

$goodsId = 0;

$goodsInfo = mGoodsBasicInfo::get('*', [
	'goods_id[>]' => $goodsId,
	'status' => [
		mGoodsBasicInfo::STATUS_PASSED,
		mGoodsBasicInfo::STATUS_ONLINE,
		mGoodsBasicInfo::STATUS_ADM_OFFLINE,
		mGoodsBasicInfo::STATUS_OFFLINE,
		mGoodsBasicInfo::STATUS_WAIT_AUDIT,
	]
]);

while ($goodsInfo) {
	$goodsId = $goodsInfo['goods_id'];
	$goodsInfo = \Biz\service\GoodsDetail::formatBasicInfos([$goodsId])[$goodsId];
	$shopInfo = \Doraemon\service\User::getUser($goodsInfo['shop_id']);
	$brandInfo = $goodsInfo['brand_id'] ?  \Biz\service\brand\Brand::getBrandInfoById($goodsInfo['brand_id']) : '';

	echo $goodsId . "\r\n";

	$item = [
		$goodsInfo['shop_id'],
		$shopInfo['nick'],
		$goodsInfo['goods_id'],
		$goodsInfo['title'],
		$brandInfo ?  $brandInfo['brand_name'] : '',
		implode('/',  array_column( $goodsInfo['category'], 'cate_name')),
		$goodsInfo['min_price'] . " - " . $goodsInfo['max_price'],
		\Doraemon\model\PhotoRelaGoods::getCounterByGoodId($goodsInfo['wiki_id']),
		Cart::admGetAllNums(GoodsSkuId::getSkuIds([$goodsInfo['goods_id']])[$goodsInfo['goods_id']]),
		$goodsInfo['status_desc']
	];
	fputcsv($fp, $item);

	$goodsInfo = mGoodsBasicInfo::get('*', [
		'goods_id[>]' => $goodsId,
		'status' => [
			mGoodsBasicInfo::STATUS_PASSED,
			mGoodsBasicInfo::STATUS_ONLINE,
			mGoodsBasicInfo::STATUS_ADM_OFFLINE,
			mGoodsBasicInfo::STATUS_OFFLINE,
			mGoodsBasicInfo::STATUS_WAIT_AUDIT,
		]
	]);
}

fclose($fp);

